package com.ycsj.goldmedalnewconcept.main.activity;

import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;

import com.ycsj.common.base.BaseActivity;
import com.ycsj.common.utils.UiUtils;
import com.ycsj.common.view.vp.SuperViewPager;
import com.ycsj.goldmedalnewconcept.R;
import com.youth.banner.transformer.DepthPageTransformer;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 引导页面
 * Created by Administrator on 2018/1/11 0011.
 */

public class GuideActivity extends BaseActivity implements ViewPager.OnPageChangeListener {

    @BindView(R.id.vp_glide)
    SuperViewPager mVpGlide;

    @BindView(R.id.ll_point)
    LinearLayout mLlPoint;

    @BindView(R.id.bt_go_main)
    Button mBtGoMain;


    //需要加载的图片资源
    private int[] glideIdes = {R.drawable.guild1, R.drawable.guild2, R.drawable.guild3, R.drawable.guild4};

    //线性布局参数
    private LinearLayout.LayoutParams lp;
    private VpAdapter adapter;

    @Override
    protected int getContentLayout() {
        return R.layout.main_activity_guide;
    }

    @Override
    protected void init() {

        //初始化底部导航的点
        initPoint();

        //初始化VP
        initVp();

        //设置监听的方法
        initEvent();
    }

    /**
     * 初始化底部导航点的方法
     */
    private void initPoint() {
        lp = new LinearLayout.LayoutParams(UiUtils.dp2px(12), UiUtils.dp2px(12));
        for (int i = 0; i < glideIdes.length; i++) {
            View view = new View(this);
            //初次加载让第一个点显示为红色
            if (i == 0) {
                lp.leftMargin = 0;
                view.setBackgroundResource(R.drawable.point_select);
            } else {
                view.setBackgroundResource(R.drawable.point_normal);
                lp.leftMargin = UiUtils.dp2px(10);
            }
            mLlPoint.addView(view, lp);
        }

        //美工给了图这里就不显示点了
        mLlPoint.setVisibility(View.GONE);

    }

    /**
     * 初始化Vp的方法
     */
    private void initVp() {
        adapter = new VpAdapter();
        mVpGlide.setAdapter(adapter);
        mVpGlide.setPageTransformer(true, new DepthPageTransformer());
    }

    /**
     * Vp设置监听的方法
     */
    private void initEvent() {
        mVpGlide.addOnPageChangeListener(this);
    }


    /**
     * 点击事件
     *
     * @param v
     */
    @OnClick({R.id.bt_go_main})
    public void clickEvent(View v) {
        int id = v.getId();
        if (id == R.id.bt_go_main) {
            //进入到主页的点击事件
            goMain();
        }
    }

    /**
     * 跳转到主界面的方法
     */
    private void goMain() {
        //跳转到角色选择界面
        UiUtils.startActivity(this, ChooseRoleActivity.class);
        finish();
    }

    /**
     * Vp的选中监听的方法
     *
     * @param position
     * @param positionOffset
     * @param positionOffsetPixels
     */
    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

    }

    @Override
    public void onPageSelected(int position) {
        //最后一个
        if (position == glideIdes.length - 1) {
            mBtGoMain.setVisibility(View.VISIBLE);
        } else {
            mBtGoMain.setVisibility(View.GONE);
        }
        syncPointState(position);
    }


    @Override
    public void onPageScrollStateChanged(int state) {

    }


    /**
     * 根据位置同步小红点的方法
     *
     * @param position
     */
    private void syncPointState(int position) {
        int childCount = mLlPoint.getChildCount();
        for (int i = 0; i < childCount; i++) {
            if (i == position) {
                mLlPoint.getChildAt(position).setBackgroundResource(R.drawable.point_select);
            } else {
                mLlPoint.getChildAt(i).setBackgroundResource(R.drawable.point_normal);
            }
        }
    }


    /**
     * ViewPager的适配器
     */
    class VpAdapter extends PagerAdapter {

        @Override
        public int getCount() {
            return glideIdes.length;
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }


        @Override
        public Object instantiateItem(ViewGroup container, int position) {
            ImageView lv = new ImageView(container.getContext());
            lv.setScaleType(ImageView.ScaleType.FIT_XY);
            lv.setImageResource(glideIdes[position]);
            container.addView(lv);
            return lv;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            container.removeView((ImageView) object);
        }
    }
}
